/**
 * 
 */
package com.kovell.profileo.servicebean;

import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

import javax.ejb.Stateless;

import com.kovell.profileo.entity.SessionEntity;
import com.kovell.profileo.entity.UserEntity;
import com.kovell.profileo.exception.EntityException;
import com.kovell.profileo.exception.ServiceException;
import com.kovell.profileo.model.UserStatus;
import com.kovell.profileo.service.UserManagementService;
import com.kovell.profileo.utils.Helper;
import com.kovell.profileo.vo.User;
import com.kovell.util.Encoder;

/**
 * @author SOSSERES
 *
 */
@Stateless(name="UserManagementServiceBean", mappedName="UserManagementServiceBean")
public class UserManagementServiceBean extends BaseEntityService implements UserManagementService{

	/**
	 * 
	 */
	public UserManagementServiceBean() {
		// TODO Auto-generated constructor stub
	}

	public User login(String userId, String password) throws ServiceException {
		
		User user = null;
		
		try {
			UserEntity userEntity = find(UserEntity.class, "login", userId);						
			
			/*
			 * business rules
			 */
			if(!userEntity.getPassword().equals(Encoder.MD5Encoder(password))){
				throw new ServiceException("PFO-101");
			}
			
			if(!UserStatus.ACTIVE.equals(userEntity.getStatus())){
				System.out.println("User status: " + userEntity.getSessionId());
				throw new ServiceException("PFO-102");
			}
			
//			if(userEntity.getFirstLogin() == null){
//				
//			}
			
			/*
			 * business logic
			 */
			
			String sessionId = UUID.randomUUID().toString();
			
			SessionEntity sessionEntity = new SessionEntity();
			sessionEntity.setId(sessionId);
			sessionEntity.setUserId(userEntity.getLogin());
			
//			update new session ID & login time
			userEntity.setSessionId(sessionId);
			userEntity.setLastLogin(new Date());
			
			update(userEntity);
			create(sessionEntity);

			user = new User();
			user.setLastLogin(Helper.convertTimestamp(userEntity.getLastLogin().getTime(), "dd-MM-yyyy HH:mm:SS"));
			user.setName(userEntity.getLastName());
//			user.setParasite(false);
			user.setSessionId(sessionId);
			
			return user;
			
		} catch (EntityException e) {
			e.printStackTrace();
			
			if(EntityException.NO_RESULT_EXCEPTION.equals(e.getExceptionType())){
				throw new ServiceException("PFO-100");
			}
			else{
				throw new ServiceException("PFO-001");
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw new ServiceException("PFO-003");
		}		
	}
	
	public void register(String userId, String password, String lastName) throws ServiceException{
						
		try {
			UserEntity userEntity = new UserEntity();
			userEntity.setLastName(lastName);
			userEntity.setLogin(userId);
			userEntity.setPassword(Encoder.MD5Encoder(password));
			userEntity.setStatus(UserStatus.ACTIVE);
			
			create(userEntity);
		} catch (EntityException e) {
			e.printStackTrace();			
			
			if(e.getExceptionType().equals(EntityException.UNIQUE_CONSTRAINT_EXCEPTION)){
				throw new ServiceException("PFO-103");
			}
			else{
				throw new ServiceException("PFO-001");
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			throw new ServiceException("PFO-003");
		}
	}
	
}
